import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/Layout.vue'
import HealthCheckList from '../views/health/HealthCheckList.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' }
      },
      // 学生和教师路由
      {
        path: 'appointment',
        name: 'Appointment',
        meta: { title: '预约管理' },
        children: [
          {
            path: 'list',
            name: 'AppointmentList',
            component: () => import('@/views/appointment/AppointmentList.vue'),
            meta: { title: '预约挂号' }
          },
          {
            path: 'my',
            name: 'MyAppointment',
            component: () => import('@/views/appointment/MyAppointment.vue'),
            meta: { title: '我的预约' }
          }
        ]
      },
      // 医生路由
      {
        path: 'doctor',
        name: 'Doctor',
        meta: { title: '医生管理' },
        children: [
          {
            path: 'appointments',
            name: 'DoctorAppointments',
            component: () => import('@/views/appointment/DoctorAppointmentList.vue'),
            meta: { title: '预约管理' }
          },
          {
            path: 'schedule',
            name: 'DoctorSchedule',
            component: () => import('@/views/schedule/DoctorSchedule.vue'),
            meta: { title: '我的排班' }
          },
          {
            path: 'list',
            name: 'DoctorList',
            component: () => import('@/views/doctor/DoctorList.vue'),
            meta: { title: '医生列表', roles: ['ADMIN'] }
          }
        ]
      },
      // 管理员路由
      {
        path: 'department',
        name: 'Department',
        meta: { title: '科室管理' },
        children: [
          {
            path: 'list',
            name: 'DepartmentList',
            component: () => import('@/views/department/DepartmentList.vue'),
            meta: { title: '科室列表' }
          }
        ]
      },
      {
        path: 'doctors',
        name: 'Doctors',
        meta: { title: '医生管理' },
        children: [
          {
            path: 'list',
            name: 'DoctorsList',
            component: () => import('@/views/doctor/DoctorList.vue'),
            meta: { title: '医生列表', roles: ['ADMIN'] }
          }
        ]
      },
      {
        path: 'schedule',
        name: 'Schedule',
        meta: { title: '排班管理' },
        children: [
          {
            path: 'management',
            name: 'ScheduleManagement',
            component: () => import('@/views/schedule/ScheduleManagement.vue'),
            meta: { title: '排班管理' }
          }
        ]
      },
      // 健康服务相关路由
      {
        path: 'health-check',
        name: 'HealthCheck',
        component: () => import('@/views/health/HealthCheckList.vue'),
        meta: { 
          title: '健康打卡',
          icon: 'Clock',
          roles: ['ADMIN', 'STUDENT', 'TEACHER', 'DOCTOR'],
          requiresAuth: true 
        }
      },
      // 论坛路由
      {
        path: 'forum',
        name: 'Forum',
        meta: {
          title: '健康论坛',
          icon: 'ChatDotRound',
          roles: ['ADMIN', 'STUDENT', 'TEACHER', 'DOCTOR'],
          requiresAuth: true,
          alwaysShow: true
        },
        children: [
          {
            path: '',
            name: 'ForumList',
            component: () => import('@/views/forum/ForumList.vue'),
            meta: { title: '浏览论坛' }
          },
          {
            path: 'postlist',
            name: 'PostList',
            component: () => import('@/views/forum/PostList.vue'),
            meta: { title: '论坛列表' }
          },
          {
            path: 'post/:id',
            name: 'PostDetail',
            component: () => import('@/views/forum/ForumPostDetail.vue'),
            meta: { 
              title: '帖子详情',
              activeMenu: '/forum',
              hidden: true
            }
          },
          {
            path: 'my-posts',
            name: 'MyPosts',
            component: () => import('@/views/forum/MyPosts.vue'),
            meta: { title: '我的帖子' }
          },
          {
            path: 'favorites',
            name: 'UserFavorites',
            component: () => import('@/views/forum/UserFavorites.vue'),
            meta: { title: '我的收藏' }
          },
          {
            path: 'category',
            name: 'PostCategory',
            component: () => import('@/views/forum/PostCategory.vue'),
            meta: { 
              title: '分类管理',
              roles: ['ADMIN']
            }
          },
          {
            path: 'management',
            name: 'ForumManagement',
            component: () => import('@/views/forum/ContentManagement.vue'),
            meta: { 
              title: '论坛管理',
              roles: ['ADMIN']
            }
          }
        ]
      },
      // 健康打卡路由
      /*{
        path: 'health',
        name: 'Health',
        meta: { title: '健康打卡', icon: 'Clock' },
        children: [
          {
            path: 'check',
            name: 'HealthCheck',
            component: HealthCheckList,
            meta: { title: '健康打卡' }
          }
        ]
      },*/
      // 社团活动路由
      {
        path: 'club',
        name: 'Club',
        meta: { 
          title: '社团活动',
          icon: 'Calendar',
          roles: ['ADMIN', 'STUDENT', 'TEACHER', 'DOCTOR'],
          requiresAuth: true,
          alwaysShow: true
        },
        children: [
          {
            path: 'list',
            name: 'ClubList',
            component: () => import('@/views/club/ClubList.vue'),
            meta: { title: '活动列表' }
          },
          {
            path: 'detail/:id',
            name: 'ClubDetail',
            component: () => import('@/views/club/ClubDetail.vue'),
            meta: { 
              title: '活动详情',
              activeMenu: '/club/list',
              hidden: true
            }
          },
          {
            path: 'my-activities',
            name: 'MyActivities',
            component: () => import('@/views/club/MyActivities.vue'),
            meta: { 
              title: '我的活动',
              roles: ['STUDENT', 'TEACHER', 'DOCTOR']
            }
          },
          {
            path: 'manage',
            name: 'ManageActivities',
            component: () => import('@/views/club/ManageActivities.vue'),
            meta: { 
              title: '活动管理', 
              roles: ['ADMIN'] 
            }
          },
          {
            path: 'publish',
            name: 'PublishActivity',
            component: () => import('@/views/club/PublishActivity.vue'),
            meta: { 
              title: '发布活动', 
              roles: ['ADMIN'] 
            }
          },
          // 保留原路径作为别名
          {
            path: 'my-registrations',
            redirect: '/club/my-activities',
            meta: { hidden: true }
          },
          {
            path: 'management',
            redirect: '/club/manage',
            meta: { hidden: true }
          }
        ]
      },
      // 个人中心
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue'),
        meta: { title: '个人中心' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由映射表，用于修复路径
const routeMapping = {
  '/health/check': '/health-check',
  '/forum/list': '/forum',
  '/club/management': '/club/manage',
  '/club/my-registrations': '/club/my-activities'
}

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 校医院预约系统` : '校医院预约系统'
  
  // 检查是否需要修复路径
  if (routeMapping[to.path]) {
    // 重定向到正确路径
    next({ path: routeMapping[to.path], replace: true })
    return
  }
  
  // 检查是否需要登录，注册页面不需要登录
  const token = localStorage.getItem('token')
  if (to.path === '/login' || to.path === '/register') {
    if (token) {
      next('/')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router 